package sales.managedbean;

import accountManagement.entity.Employee;
import accountManagement.entity.Salesman;
import accountManagement.sessionbean.SalesmanManager;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;
import sales.entity.SalesOrderEntity;
import sales.seesion.stateless.SalesOrderSessionBean;
import sales.util.exception.NoPurchaseOrderException;

/**
 *
 * @author xhx
 */
@ManagedBean
@ViewScoped
public class ViewPendingOrdersManagedBean {

    private SalesOrderEntity selectedPurchaseOrder;
    private Long userId;
    private Salesman employee;
    @EJB
    SalesOrderSessionBean salesOrderSessionBean;
    @EJB
    SalesmanManager employeeManager;

    /** Creates a new instance of ViewAllSalesOrderManagedBean */
    public ViewPendingOrdersManagedBean() {
        selectedPurchaseOrder = new SalesOrderEntity();

    }

    public List<SalesOrderEntity> getPurchaseOrderList() throws IOException {
        List<SalesOrderEntity> result = new ArrayList();
        HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
        userId = (Long) request.getSession().getAttribute("userId");
        employee = employeeManager.getSalesmanById(userId);
        try {
            result = salesOrderSessionBean.getAllPendingOrders(employee);
        } catch (NoPurchaseOrderException e) {
            FacesContext.getCurrentInstance().getExternalContext().redirect("error/error_noPurchaseOrder.xhtml");
        }
        return result;
    }

    public void viewPurchaseOrder(SalesOrderEntity salesOrder) throws IOException {
        selectedPurchaseOrder = salesOrder;
        String lastURL = "viewPendingOrders.xhtml";
          
        FacesContext.getCurrentInstance().getExternalContext().getFlash().put("purchaseOrder", selectedPurchaseOrder);
        FacesContext.getCurrentInstance().getExternalContext().getFlash().put("lastURL", lastURL);  
        FacesContext.getCurrentInstance().getExternalContext().redirect("displayPurchaseOrder.xhtml");
    }

    public void confirmPurchaseOrder(SalesOrderEntity salesOrder) throws IOException {
        selectedPurchaseOrder = salesOrder;
        String lastURL = "viewPendingOrders.xhtml";
        FacesContext.getCurrentInstance().getExternalContext().getFlash().put("lastURL", lastURL);
        System.out.println(lastURL + (String)FacesContext.getCurrentInstance().getExternalContext().getFlash().get("lastURL"));
        FacesContext.getCurrentInstance().getExternalContext().getFlash().put("purchaseOrder", selectedPurchaseOrder);
        FacesContext.getCurrentInstance().getExternalContext().redirect("confirmPendingOrder.xhtml");
    }

//    public void verifySalesOrder(SalesOrderEntity salesOrder) throws IOException{
//       selectedPurchaseOrder = salesOrder;
//       FacesContext.getCurrentInstance().getExternalContext().getFlash().put("newSalesOrder", selectedPurchaseOrder);
//       FacesContext.getCurrentInstance().getExternalContext().redirect("verifySalesOrderResult.xhtml");   
//    }
    public SalesOrderEntity getSelectedPurchaseOrder() {
        return selectedPurchaseOrder;
    }

    public void setSelectedPurchaseOrder(SalesOrderEntity selectedPurchaseOrder) {
        this.selectedPurchaseOrder = selectedPurchaseOrder;
    }

    public Long getUserId() {
        return userId;
    }

    public void setUserId(Long userId) {
        this.userId = userId;
    }

    public Salesman getEmployee() {
        return employee;
    }

    public void setEmployee(Salesman employee) {
        this.employee = employee;
    }
}
